#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "MSTK_util.h"

#ifdef __cplusplus
extern "C" {
#endif

  void MSTK_Report(const char *funcname, const char *message, ErrType type) {
    static char last_message[1024];
    int len;

    if (strcmp(last_message,message) == 0)
      return;

    
    len = strlen(message);
    memcpy(last_message,message,len*sizeof(char));
    last_message[len] = '\0';

    switch (type) {
    case MESG:
      fprintf(stdout, "%s: %s\n",funcname,message);
      break;
    case WARN:
      fprintf(stderr, "Warning!! in %s: %s\n",funcname,message);
      break;
    case ERROR:
      fprintf(stderr, "ERROR!! in %s: %s\n",funcname,message);
      break;
    case FATAL:
      fprintf(stderr, "FATAL ERROR!! in %s: %s\n",funcname,message);
      exit(-1);
    }
  }


 void itoa(int n, char s[]) {
    int i, sign;
    sign = n;
    
    i = 0;
    do {
      s[i++] = abs(n % 10) + '0';
    } while ( n /= 10 );
    if (sign < 0)
      s[i++] = '-';
    
    s[i] = '\0';
    reverse(s);
  }
  void reverse(char s[]) {
    int c, i, j;
    for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
      c = s[i];
      s[i] = s[j];
      s[j] = c;
    }
  }  

#ifdef __cplusplus
}
#endif
